package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.SyslogDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.SyslogService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName SyslogController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 14:42:16
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/syslog")
public class SyslogController {

    private static final Logger LOG = LoggerFactory.getLogger(SyslogController.class);
    public static final String BUSINESS_NAME = "系统日志";

    @Resource
    private SyslogService syslogService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        syslogService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody SyslogDto syslogDto) {
        // 保存校验
        ValidatorUtil.require(syslogDto.getId(), "");
        ValidatorUtil.length(syslogDto.getId(), "", 1, 50);
        ValidatorUtil.length(syslogDto.getContent(), "日志内容", 1, 1000);
        ValidatorUtil.length(syslogDto.getTypename(), "分类名称", 1, 20);
        ValidatorUtil.length(syslogDto.getTypeid(), "分类ID", 1, 50);
        ValidatorUtil.length(syslogDto.getHref(), "操作所属模块地址", 1, 200);
        ValidatorUtil.require(syslogDto.getCreatetime(), "记录时间");
        ValidatorUtil.require(syslogDto.getCreateid(), "操作人ID");
        ValidatorUtil.length(syslogDto.getCreateid(), "操作人ID", 1, 50);
        ValidatorUtil.length(syslogDto.getCreatename(), "操作人", 1, 200);
        ValidatorUtil.length(syslogDto.getIp(), "操作机器的IP地址", 1, 20);
        ValidatorUtil.require(syslogDto.getResult(), "操作的结果：0：成功；1：失败；");
        ValidatorUtil.length(syslogDto.getApplication(), "所属应用", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        syslogService.save(syslogDto);
        responseDto.setContent(syslogDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        syslogService.delete(id);
        return responseDto;
    }
}
